<script>
import { mixinFetchEmp } from '@/mixin'

export default {
  name: 'employee',
  mixins: [mixinFetchEmp],
  data() {
    return {
      /* 获取员工分页参数
      pageParams: {
        name: undefined,
        page: 1,
        pageSize: 10
      },
      */
      formData: {
        name: '',
        username: '',
        idNumber: '',
        sex: '',
        phone: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入员工账号', trigger: 'blur' }
        ],
        idNumber: [
          { required: true, message: '请输入员工身份证', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择员工性别', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入员工手机号', trigger: 'blur' }
        ]
      },
      formRef: null,
      edit: false,
      dialogVisible: false
    }
  },
  methods: {
    /*
    获取员工列表的回调函数
    async fetchEmployeeList() {
      try {
        await this.$store.dispatch('employeeStore/fetchEmployeeListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    */

    /*
    分页的回调函数
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchEmployeeList()
    },
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchEmployeeList()
    },
    */

    /* 点击删除按钮的回调函数 */
    async handleDelete(employee) {
      const confirm = await this.$confirm(`确认删除员工${employee.name}吗?`, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('employeeStore/deleteEmployeeAction', employee.id)
        this.fetchEmployeeList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    /* 更新员工状态的回调函数 */
    async updateStatus(status, id) {
      try {
        const newStatus = status === 1 ? 0 : 1
        const updateMessage = await this.$store.dispatch('employeeStore/updateStatusAction', { status: newStatus, id })
        this.fetchEmployeeList()
        this.$message.success(updateMessage)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 添加员工的回调函数 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 编辑员工的回调函数 */
    async handleEdit(employee) {
      this.edit = true
      this.dialogVisible = true
      const employeeInfo = await this.$store.dispatch('employeeStore/fetchEmployeeDetailAction', employee.id)
      this.formData = employeeInfo
    },
    /* 点击查询员工按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchEmployeeList()
    },

    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      const validate = await this.$refs.formRef.validate().catch(error => error)
      if (!validate) return
      try {
        const message = await this.$store.dispatch('employeeStore/addOrUpdateEmployee', this.formData)
        this.dialogVisible = false
        this.fetchEmployeeList()
        this.$message.success(message)
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  computed: {
    //...mapState('employeeStore', ['total', 'records']),
    title() {
      return this.edit ? '编辑员工' : '添加员工'
    }
  }
  /*
  mounted() {
    this.fetchEmployeeList()
  }
  */
}
</script>

<template>
  <div class="employee-container">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <el-form inline size="small">
          <el-form-item label="员工姓名:">
            <el-input placeholder="请输入员工的姓名" v-model="pageParams.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" size="small" icon="el-icon-search" @click="handleSearch">查询员工</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加员工</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table size="small" stripe border :data="records">
        <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
        <el-table-column prop="id" label="员工ID" align="center"></el-table-column>
        <el-table-column prop="name" label="员工姓名" align="center"></el-table-column>
        <el-table-column label="员工性别" align="center">
          <template #default="{row}">
            {{ row.sex === '1' ? '男' : '女' }}
          </template>
        </el-table-column>
        <el-table-column prop="username" label="账号" align="center"></el-table-column>
        <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
        <el-table-column label="账号状态" align="center">
          <template #default="{row}">
            <el-switch v-model="row.status" disabled :active-value="1" :inactive-value="0" active-text="启用" inactive-text="禁用" active-color="#13ce66"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="操作时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">修改</el-button>
            <el-button type="text" @click="updateStatus(row.status,row.id)">{{ row.status === 1 ? '禁用' : '启用' }}</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" @close="$refs.formRef.resetFields()">
      <el-form label-width="100px" :model="formData" :rules="formRules" ref="formRef">
        <el-form-item label="员工账号:" prop="username">
          <el-input placeholder="请输入员工的账号" v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名:" prop="name">
          <el-input placeholder="请输入员工的姓名" v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input placeholder="请输入员工的手机号" v-model="formData.phone"></el-input>
        </el-form-item>
        <el-form-item label="员工性别" prop="sex">
          <el-radio v-model="formData.sex" label="1">男</el-radio>
          <el-radio v-model="formData.sex" label="0">女</el-radio>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input placeholder="请输入员工的身份证号" v-model="formData.idNumber"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" size="small" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.employee-container {
  padding: 25px;


  .el-table {
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
